<template>
  <div class="employees">
    <el-card class="btn">
      <el-button
        v-control="'employees_import_btn'"
        type="primary"
        @click="goImport"
      >导入</el-button>
      <el-button type="primary">导出</el-button>
      <el-button type="primary" @click="add">+新增员工</el-button>
    </el-card>
    <el-card>
      <el-table :data="list">
        <el-table-column label="序号" width="50px">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="头像" prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              v-imgerror="errorUrl"
              :src="row.staffPhoto"
              class="avatar"
              alt=""
              @click="imgClick(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />
        <!-- :formatter="formatter" -->
        <el-table-column label="聘用形式" prop="formOfEmployment">
          <!-- 使用过滤器 -->
          <!-- <template v-slot="{ row }">
            <div v-hireType="row.formOfEmployment">
              {{ row.formOfEmployment | formatType }}
            </div>
          </template> -->
        </el-table-column>
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column label="入职时间" prop="timeOfEntry">
          <template v-slot="{ row }">
            <div>
              {{ row.timeOfEntry | formatTime }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="账户状态" prop="enableState" />
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="goDetails(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="roleClick(row.id)">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 10, 100, 200, 300, 400]"
          :page-size="page.size"
          layout="total, prev, pager, next"
          :total="page.total"
          @size-change="sizeChange"
          @current-change="currentChange"
        />
      </div>
    </el-card>
    <DialogItem :is-show.sync="show" @getData="getData" />
    <el-dialog title="二维码" :visible.sync="showQrcode">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
    <DialogRole ref="dialogrole" />
  </div>
</template>

<script>
// 获取员工列表，删除api
import { sysUser, delSysUser } from '@/api/employees.js'
import employees from '@/assets/constant/employees'
import DialogItem from './components/dialog-item.vue'
import QrCode from 'qrcode'
// 导入分配角色组件
import DialogRole from './components/dialogrole.vue'
export default {
  name: 'Employees',
  components: {
    DialogItem,
    DialogRole
  },
  data() {
    return {
      showQrcode: false,
      errorUrl: require('@/assets/common/head.jpg'),
      show: false,
      list: [],
      page: {
        page: 1,
        size: 5,
        total: 10
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.page)
      console.log(res)
      // 原始数据转换
      res.data.rows.forEach((item) => {
        item.formOfEmployment = this.formatter(
          null,
          null,
          +item.formOfEmployment
        )
      })
      this.list = res.data.rows
      this.page.total = res.data.total
    },
    // 每页多少条
    sizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    // 当前页
    currentChange(page) {
      this.page.page = page
      this.getData()
    },
    // form自带的过滤器
    formatter(row, column, cellValue, index) {
      const result = employees.hireType.find((item) => {
        return item.id === cellValue
      })
      return result ? result.value : '-'
    },
    add() {
      this.show = true
    },
    del(id) {
      this.$confirm('确定要删除该员工吗？', '提示').then(async() => {
        await delSysUser(id)
        this.$message.success('删除成功！')
        // 回到当前页
        this.currentChange(1)
      })
    },
    goImport() {
      this.$router.push(
        '/employees/import?name=' +
          encodeURIComponent('员工') +
          `&redirect=${encodeURIComponent('/employees')}`
      )
    },
    goDetails(id) {
      this.$router.push('/employees/details/' + id)
    },
    imgClick(imgUrl) {
      if (!imgUrl) {
        this.$message.error('暂无图片')
        return
      }
      this.showQrcode = true
      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.canvas, imgUrl, {
          width: 350,
          height: 350
        })
      })
    },
    roleClick(id) {
      this.$refs.dialogrole.isShow = true
      this.$refs.dialogrole.getUserInfo(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.btn {
  padding-bottom: 15px;
  text-align: right;
}
.page {
  padding-top: 15px;
  text-align: right;
}
.avatar {
  width: 30px;
  height: 30px;
}
</style>
